﻿//AcademicPlanner - course registration planning web app.
//Copyright (C) 2009  Boubacar Diallo

//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either version 2
//of the License, or (at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using AcademicPlanner.Domain.Models;
using AcademicPlanner.Framework.Helpers;

namespace AcademicPlanner.Presentation.Views.Shared
{
	/// <summary>
	/// Possible scenarios of a <c>AccountForm</c> form.
	/// </summary>
	/// <version>1.0</version>
	/// <since>1.0</since>
	public enum AccountFormScenario
	{
		NEW,
		LOGIN,
		EDIT
	}


	/// <summary>
	/// AccountFormModel represents a model object for a <c>AccountForm</c> form.
	/// </summary>
	/// <version>1.0</version>
	/// <since>1.0</since>
	public class AccountFormModel : FormModel
	{
		/// <summary>
		/// Gets or sets the scenario to use the <c>AccountsForm</c> form under.
		/// </summary>
		/// <version>1.0</version>
		/// <since>1.0</since>
		public AccountFormScenario Scenario { get; set; }

		/// <summary>
		/// Gets or sets the <c>Account</c> whose data to save.
		/// </summary>
		/// <version>1.0</version>
		/// <since>1.0</since>
		public Account Account { get; set; }
	}


	/// <summary>
	/// AccountForm represents a form to save data about an <c>Account</c>.
	/// </summary>
	/// <version>1.0</version>
	/// <since>1.0</since>
	public partial class AccountForm : Form<AccountFormModel>
	{
		/// <summary>
		/// Gets the model object for this form control.
		/// </summary>
		/// <version>1.0</version>
		/// <since>1.0</since>
		public new AccountFormModel Model { get { return (AccountFormModel)base.Model; } }

		/// <summary>
		/// Gets or sets the value of the user name field of this form.
		/// </summary>
		/// <version>1.0</version>
		/// <since>1.0</since>
		public string UserNameValue { get; set; }

		/// <summary>
		/// Gets or sets the value of the user password field of this form.
		/// </summary>
		/// <version>1.0</version>
		/// <since>1.0</since>
		public string UserPasswordValue { get; set; }

		/// <summary>
		/// Gets or sets the value of the remember me field in this form;
		/// used in <c>LOGIN</c> scenario.
		/// </summary>
		/// <version>1.0</version>
		/// <since>1.0</since>
		public bool RememberMeValue { get; set; }


		/// <summary>
		/// Handles the load page event. This method initializes or customizes the form's properties
		/// based on the model object's <c>Scenario</c>.
		/// </summary>
		/// <param name="sender">The sender of the event.</param>
		/// <param name="e">The details of the event.</param>
		/// <version>1.0</version>
		/// <since>1.0</since>
		protected void Page_Load(object sender, EventArgs e)
		{
			method = FormMethod.Post;

			if (Model.Scenario.Equals(AccountFormScenario.LOGIN))
			{
				actionName = "Signin";
				controllerName = "Site";
				routeValues = new object { };
				Name = "loginAccountForm";
				Title = Tr("Login");
				SubmitButtonValue = Tr("Login");
			}
			else if (Model.Scenario.Equals(AccountFormScenario.EDIT))
			{
				actionName = "Save";
				controllerName = "Accounts";
				routeValues = new { pId = Model.Account.EntityId };
				Title = Tr("Change Account Password");
				Name = "saveAccountForm";
				SubmitButtonValue = Tr("Save");
			}
			else
			{
				actionName = "";
				controllerName = "";
				routeValues = new object { };
				Title = Tr("Account Information");
				Name = "saveFirstAccountForm";
				SubmitButtonValue = Tr("Save Account");
			}

			if (string.IsNullOrEmpty(Model.Id)) Model.Id = Name;

			if (Model.Scenario.Equals(AccountFormScenario.LOGIN))
			{
				var vCookie = Context.Request.Cookies
					.Get((string)ConfigHelper.GetPresentationSectionValue("loginSettings", "usernameCookieName"));

				UserNameValue = vCookie != null 
								? (string)vCookie.Value
								: (string)ConfigHelper.GetPresentationSectionValue("loginSettings", "defaultUsername");
				vCookie = Context.Request.Cookies
					.Get((string)ConfigHelper.GetPresentationSectionValue("loginSettings", "passwordCookieName"));
				UserPasswordValue = vCookie != null 
									? (string)vCookie.Value
									: (string)ConfigHelper.GetPresentationSectionValue("loginSettings", "defaultPassword"); ;
				RememberMeValue = true;
			}
			else if (Model.Scenario.Equals(AccountFormScenario.EDIT))
			{
				UserNameValue = Model.Account.User.Name;
				UserPasswordValue = "";
			}
			else
			{
				UserNameValue = "";
				UserPasswordValue = "";
			}
		}
	}
}